package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.*;
import com.jyy.prefabricated.enums.CourseTypeEnum;
import com.jyy.prefabricated.enums.ExtractingStatusEnum;
import com.jyy.prefabricated.enums.OrderTypeEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.*;
import com.jyy.prefabricated.utils.UserSessionUtil;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.PracticeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ExamPaperService {

    @Resource
    private ExamPaperMapper paperMapper;

    @Resource
    private ExamPaperAreaService areaService;

    @Resource
    private ExamPaperGoodsMapper examPaperGoodsMapper;

    @Resource
    private AreaQuestionMapper areaQuestionDao;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private ProjectCategoryMapper projectCategoryMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    @Resource
    private CoursePackageMapper coursePackageMapper;

    @Resource
    private UserUtils userUtils;

    @Resource
    private MyResourceService myResourceService;

    @Resource
    private CoursePackageService coursePackageService;

    @Resource
    private TrainingOrganizationStockService trainingOrganizationStockService;

    /**
     * 新增
     *
     * @param paper
     * @return
     */
    public int insert(ExamPaper paper) {
        paperMapper.insert(paper);
        //savePaperId2Area(paper.getAreas(), paper.getId());
        addPaperId2Area(paper.getAreas(), paper.getId());

        //线下课程
        if (paper.getCourseList().size() > 0) {
            for (int i = 0; i < paper.getCourseList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCourseList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //录播课程
        if (paper.getRecordingCoursesList().size() > 0) {
            for (int i = 0; i < paper.getRecordingCoursesList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getRecordingCoursesList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //文档课程
        if (paper.getDocumentCoursesList().size() > 0) {
            for (int i = 0; i < paper.getDocumentCoursesList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getDocumentCoursesList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //关联课程包
        if (paper.getCoursePackageList().size() > 0) {
            for (int i = 0; i < paper.getCoursePackageList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCoursePackageList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE_PACKAGE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //关联项目
        if (paper.getProjectList().size() > 0) {
            for (int i = 0; i < paper.getProjectList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCourseList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.PROJECT.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        if (paper.getAreas().size() > 0) {
            for (ExamPaperArea examPaperArea : paper.getAreas()) {
                if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.AUTONOMY.getValue())) {
                    //自主出题
                    areaQuestionDao.deleteByAreaId(examPaperArea.getId());
                    for (int i = 0; i < examPaperArea.getQuestionList().size(); i++) {
                        AreaQuestion areaQuestion = new AreaQuestion();
                        areaQuestion.setAreaId(examPaperArea.getId());
                        areaQuestion.setQuestionId(examPaperArea.getQuestionList().get(i));
                        areaQuestionDao.insert(areaQuestion);
                    }
                }
            }
        }
        ExamPaper result = paperMapper.selectByPrimaryKey(paper.getId());
        //添加区域题目表信息
        if (result.getAreas().size() > 0) {
            for (ExamPaperArea examPaperArea : result.getAreas()) {
                //顺序出题
                if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.ORDER.getValue())) {
                    if (examPaperArea.getRules().size() > 0) {
                        for (ExamPaperAreaRule examPaperAreaRule : examPaperArea.getRules()) {
                            List<Question> questionList = questionMapper.selectByParams(examPaperAreaRule.getQuestionBankId(), examPaperAreaRule.getQuestionType(), examPaperAreaRule.getStemType(), null, null, examPaperAreaRule.getDifficulty(), null, null, null);
                            if(examPaperAreaRule.getQuestionCount()>questionList.size()){
                                examPaperAreaRule.setQuestionCount(questionList.size());
                            }
                            if (examPaperAreaRule.getQuestionCount() != null) {
                                for (int i = 0; i < examPaperAreaRule.getQuestionCount(); i++) {
                                    AreaQuestion areaQuestion = new AreaQuestion();
                                    areaQuestion.setAreaId(examPaperArea.getId());
                                    if (questionList != null && i < questionList.size()) {
                                        areaQuestion.setQuestionId(questionList.get(i).getId());
                                        areaQuestionDao.insert(areaQuestion);
                                    }

                                }
                            }

                        }
                    }
                } else if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.RANDOM.getValue())) {
                    //随机出题
                    if (examPaperArea.getRules().size() > 0) {
                        for (ExamPaperAreaRule examPaperAreaRule : examPaperArea.getRules()) {
                            List<Question> questionList = questionMapper.selectByParams(examPaperAreaRule.getQuestionBankId(), examPaperAreaRule.getQuestionType(), examPaperAreaRule.getStemType(), null, null, examPaperAreaRule.getDifficulty(), null, null, null);
                            Collections.shuffle(questionList);
                            //选取随机的集合元素
                            if(examPaperAreaRule.getQuestionCount()>questionList.size()){
                                examPaperAreaRule.setQuestionCount(questionList.size());
                            }
                            List<Question> randomSeries = questionList.subList(0, examPaperAreaRule.getQuestionCount());
                            if (examPaperAreaRule.getQuestionCount() != null) {
                                for (int i = 0; i < examPaperAreaRule.getQuestionCount(); i++) {
                                    AreaQuestion areaQuestion = new AreaQuestion();
                                    areaQuestion.setAreaId(examPaperArea.getId());
                                    if (randomSeries != null && i < randomSeries.size()) {
                                        areaQuestion.setQuestionId(randomSeries.get(i).getId());
                                        areaQuestionDao.insert(areaQuestion);
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
        return paper.getId();
    }

    /**
     * 保存试卷id到试卷区域列表
     *
     * @param areas
     * @param id
     */
    private void savePaperId2Area(List<ExamPaperArea> areas, Integer id) {
        if (areas.size() > 0) {
            for (ExamPaperArea area : areas) {
                area.setPaperId(id);
                areaService.update(area);
            }
        }
    }

    /**
     * 新增试卷id到试卷区域列表
     *
     * @param areas
     * @param id
     */
    private void addPaperId2Area(List<ExamPaperArea> areas, Integer id) {
        if (areas.size() > 0) {
            for (ExamPaperArea area : areas) {
                area.setPaperId(id);
                areaService.add(area);
            }
        }
    }

    /**
     * 新增试卷id到试卷区域列表
     *
     * @param areas
     */
    private void deletedPaperId2Area(List<ExamPaperArea> areas) {
        if (areas.size() > 0) {
            for (ExamPaperArea area : areas) {
                areaService.delete(area.getId());
            }
        }
    }

    /**
     * 批量修改发布状态
     *
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        return paperMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 更新
     *
     * @param paper
     * @return
     */
    public int update(ExamPaper paper) {
        if (paper.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        // savePaperId2Area(paper.getAreas(), paper.getId());
        deletedPaperId2Area(paper.getAreas());
        addPaperId2Area(paper.getAreas(), paper.getId());
        examPaperGoodsMapper.deleteBypaperId(paper.getId());
        //线下课程
        if (paper.getCourseList().size() > 0) {
            for (int i = 0; i < paper.getCourseList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCourseList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //录播课程
        if (paper.getRecordingCoursesList().size() > 0) {
            for (int i = 0; i < paper.getRecordingCoursesList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getRecordingCoursesList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //文档课程
        if (paper.getDocumentCoursesList().size() > 0) {
            for (int i = 0; i < paper.getDocumentCoursesList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getDocumentCoursesList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //关联课程包
        if (paper.getCoursePackageList().size() > 0) {
            for (int i = 0; i < paper.getCourseList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCoursePackageList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.COURSE_PACKAGE.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }
        //关联项目
        if (paper.getProjectList().size() > 0) {
            for (int i = 0; i < paper.getProjectList().size(); i++) {
                ExamPaperGoods examPaperGoods = new ExamPaperGoods();
                examPaperGoods.setPaperId(paper.getId());
                examPaperGoods.setGoodsId(paper.getCourseList().get(i));
                examPaperGoods.setGoodsType(OrderTypeEnum.PROJECT.getValue());
                examPaperGoods.setDeleted(false);
                examPaperGoodsMapper.insert(examPaperGoods);
            }
        }

        //先删除区域题目后添加
        ExamPaper result = paperMapper.selectByPrimaryKey(paper.getId());
        if (result.getAreas().size() > 0) {
            for (ExamPaperArea examPaperArea : result.getAreas()) {
                areaQuestionDao.deleteByAreaId(examPaperArea.getId());
            }
        }
        if (paper.getAreas().size() > 0) {
            for (ExamPaperArea examPaperArea : paper.getAreas()) {
                if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.AUTONOMY.getValue())) {
                    //自主出题
                    areaQuestionDao.deleteByAreaId(examPaperArea.getId());
                    for (int i = 0; i < examPaperArea.getQuestionList().size(); i++) {
                        AreaQuestion areaQuestion = new AreaQuestion();
                        areaQuestion.setAreaId(examPaperArea.getId());
                        areaQuestion.setQuestionId(examPaperArea.getQuestions().get(i).getId());
                        areaQuestionDao.insert(areaQuestion);
                    }
                }
            }
        }

        //添加区域题目表信息
        if (paper.getAreas().size() > 0) {
            for (ExamPaperArea examPaperArea : paper.getAreas()) {
                //顺序出题
                if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.ORDER.getValue())) {
                    if (examPaperArea.getRules().size() > 0) {
                        for (ExamPaperAreaRule examPaperAreaRule : examPaperArea.getRules()) {
                            List<Question> questionList = questionMapper.selectByParams(examPaperAreaRule.getQuestionBankId(), examPaperAreaRule.getQuestionType(), examPaperAreaRule.getStemType(), null, null, examPaperAreaRule.getDifficulty(), null, null, null);
                            if(examPaperAreaRule.getQuestionCount()>questionList.size()){
                                examPaperAreaRule.setQuestionCount(questionList.size());
                            }
                            if (examPaperAreaRule.getQuestionCount() != null) {
                                for (int i = 0; i < examPaperAreaRule.getQuestionCount(); i++) {
                                    AreaQuestion areaQuestion = new AreaQuestion();
                                    areaQuestion.setAreaId(examPaperArea.getId());
                                    if (questionList != null && i < questionList.size()) {
                                        areaQuestion.setQuestionId(questionList.get(i).getId());
                                        areaQuestionDao.insert(areaQuestion);
                                    }
                                }
                            }

                        }
                    }
                } else if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.RANDOM.getValue())) {
                    //随机出题
                    if (examPaperArea.getRules().size() > 0) {
                        for (ExamPaperAreaRule examPaperAreaRule : examPaperArea.getRules()) {
                            List<Question> questionList = questionMapper.selectByParams(examPaperAreaRule.getQuestionBankId(), examPaperAreaRule.getQuestionType(), examPaperAreaRule.getStemType(), null, null, examPaperAreaRule.getDifficulty(), null, null, null);
                            Collections.shuffle(questionList);
                            //选取随机的集合元素
                            if(examPaperAreaRule.getQuestionCount()>questionList.size()){
                                examPaperAreaRule.setQuestionCount(questionList.size());
                            }
                            List<Question> randomSeries = questionList.subList(0, examPaperAreaRule.getQuestionCount());
                            if (examPaperAreaRule.getQuestionCount() != null) {
                                for (int i = 0; i < examPaperAreaRule.getQuestionCount(); i++) {
                                    AreaQuestion areaQuestion = new AreaQuestion();
                                    areaQuestion.setAreaId(examPaperArea.getId());
                                    if (randomSeries != null && i < randomSeries.size()) {
                                        areaQuestion.setQuestionId(randomSeries.get(i).getId());
                                        areaQuestionDao.insert(areaQuestion);
                                    }
                                }
                            }

                        }
                    }

                }
            }

        }

        return paperMapper.updateByPrimaryKeySelective(paper);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        return paperMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     *
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        return paperMapper.batchDelete(dto.getIds());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ExamPaper selectById(Integer id) {
        ExamPaper examPaper = paperMapper.selectByPrimaryKey(id);


        if (examPaper != null) {
            if (examPaper.getAreas() != null && examPaper.getAreas().size() > 0) {
                for (ExamPaperArea examPaperArea : examPaper.getAreas()) {
                    if (examPaperArea.getExtractingStatus().equals(ExtractingStatusEnum.AUTONOMY.getValue())) {
                        examPaperArea.getQuestions().addAll(questionMapper.selectByAreaId(examPaperArea.getId()));
                    }
                }
            }
            for (ExamPaperGoods examPaperGoods : examPaper.getGoodsList()) {
                //项目
                if (examPaperGoods.getGoodsType().equals(OrderTypeEnum.PROJECT.getValue())) {
                    ProjectInfo projectInfo = projectInfoMapper.selectByPrimaryKey(examPaperGoods.getGoodsId());
                    examPaperGoods.setGoodsName(projectInfo.getName());
                    ProjectCategory projectCategory = projectCategoryMapper.selectByPrimaryKey(projectInfo.getCategoryId());
                    examPaperGoods.setCategoryName(projectCategory.getCategory());
                    examPaper.getPjList().add(projectInfo);


                }
                //课程
                if (examPaperGoods.getGoodsType().equals(OrderTypeEnum.COURSE.getValue())) {
                    CourseWithBLOBs course = courseMapper.selectByPrimaryKey(examPaperGoods.getGoodsId());
                    examPaperGoods.setGoodsName(course.getCourseName());
                    CourseCategory courseCategory = courseCategoryMapper.selectByPrimaryKey(course.getCategoryId());
                    examPaperGoods.setCategoryName(courseCategory.getCategory());
                    if (course.getType().equals(CourseTypeEnum.OFFLINE.getValue())) {
                        examPaper.getCsList().add(course);
                    } else if (course.getType().equals(CourseTypeEnum.DOCUMENT.getValue())) {
                        examPaper.getDcList().add(course);
                    } else {
                        examPaper.getRcList().add(course);
                    }
                }
                //课程包
                if (examPaperGoods.getGoodsType().equals(OrderTypeEnum.COURSE_PACKAGE.getValue())) {
                    CoursePackage coursePackage = coursePackageMapper.selectByPrimaryKey(examPaperGoods.getGoodsId());
                    examPaperGoods.setGoodsName(coursePackage.getCourseName());
                    CourseCategory courseCategory = courseCategoryMapper.selectByPrimaryKey(coursePackage.getCategoryId());
                    examPaperGoods.setCategoryName(courseCategory.getCategory());
                    examPaper.getCpList().add(coursePackage);
                }
            }
        }
        return examPaper;
    }

    /**
     * 通过参数查询列表（考试）
     *
     * @param name   试卷名称
     * @param status 状态
     * @return
     */
    public List<PracticeVo> selectByParams(String name, Integer status, Integer paperStatus, Integer trainingOrganizationId) {
        return paperMapper.selectByParams(name, status, paperStatus, trainingOrganizationId);

    }

    /**
     * 通过参数查询列表（练习）
     *
     * @param name        试卷名称
     * @param status      状态
     * @param categoryIds 分类id集合
     * @param sortStatus  排序状态（1、最新、2、最热、3、免费）
     * @return
     */
    public List<PracticeVo> selectPracticeByParams(String name, Integer status, Integer paperStatus, List<Integer> categoryIds, Integer sortStatus) {
        List<PracticeVo> result = paperMapper.selectPracticeByParams(name, status, paperStatus, categoryIds, sortStatus);
        if (result != null) {
            SysUser user = null;
            String token = UserSessionUtil.resolveToken();
            if (StringUtils.isNotBlank(token)) {
                user = userUtils.getCurrentUser();
            }
            if (user != null) {
                if (user.getStudent() != null) {
                    for (PracticeVo practiceVo : result) {
                        List<Integer> coursePackageIds = myResourceService.selectCoursePackageIdsByStudentId(user.getStudent().getId());
                        //已经购买的未过期的课程包
                        List<CoursePackage> coursePackageList = myResourceService.selectByCoursePackageNameAndCourseIds(name, coursePackageIds);
                        if (coursePackageList != null && coursePackageList.size() > 0) {
                            for (CoursePackage item : coursePackageList) {
                                //课程包包含的练习
                                CoursePackage coursePackage = coursePackageService.selectTree(item.getId());
                                List<ExamPaper> practiceList = coursePackageService.findPractices(coursePackage);
                                if (practiceList != null && practiceList.size() > 0) {
                                    List<Integer> practiceIdList = practiceList.stream().map(e -> e.getId()).collect(Collectors.toList());
                                    if (practiceIdList.contains(practiceVo.getId())) {
                                        practiceVo.setIsBuy(true);
                                        break;
                                    }
                                } else {
                                    practiceVo.setIsBuy(false);
                                }
                            }
                        } else {
                            practiceVo.setIsBuy(false);
                        }
//                        List<Integer> coursePackageIdList = queryCoursePackageIdByPracticeId(practiceVo.getId());
//                        List<String> coursePackageNameList = new ArrayList<>();
//                        if (coursePackageIdList != null && coursePackageIdList.size() > 0) {
//                            for (Integer id : coursePackageIdList) {
//                                CoursePackage cp = coursePackageMapper.selectByPrimaryKey(id);
//                                coursePackageNameList.add(cp.getCourseName());
//                            }
//                            practiceVo.setCoursePackageNameList(coursePackageNameList);
//                        }
                    }
                }
                if (user.getTrainingOrganization() != null) {
                    for (PracticeVo practiceVo : result) {
                        //查询企业已购买未过期的课程包库存
                        List<TrainingOrganizationStock> stocksList = trainingOrganizationStockService.selectByParams(null, OrderTypeEnum.COURSE_PACKAGE.getValue(), user.getTrainingOrganization().getId(), null);

                        if (stocksList != null && stocksList.size() > 0) {
                            for (TrainingOrganizationStock trainingOrganizationStock : stocksList) {
                                CoursePackage coursePackage = coursePackageService.selectTree(trainingOrganizationStock.getProductId());
                                //课程包包含的练习
                                List<ExamPaper> practiceList = coursePackageService.findPractices(coursePackage);
                                if (practiceList != null && practiceList.size() > 0) {
                                    List<Integer> practiceIdList = practiceList.stream().map(e -> e.getId()).collect(Collectors.toList());
                                    if (practiceIdList.contains(practiceVo.getId())) {
                                        practiceVo.setIsBuy(true);
                                        break;
                                    }
                                } else {
                                    practiceVo.setIsBuy(false);
                                }
                            }
                        } else {
                            practiceVo.setIsBuy(false);
                        }
                    }
                }
            }
        }
        return result;

    }

    public List<Integer> queryCoursePackageIdByPracticeId(Integer practiceId) {
        List<Integer> result = new ArrayList<>();
        return result;
    }

    /**
     * 查询试卷题目所有信息
     *
     * @param id
     * @return
     */
    public List<String> selectQuestionById(Integer id) {


        List<Question> questionList = questionMapper.selectByExamPaperId(id);

        List<String> result = new ArrayList<>();
        return result;
    }

    private void writerErrorFileContent(String fileDir, String filePath, List<String> content, String type) {
        XWPFDocument document = new XWPFDocument();
        OutputStream stream = null;
        BufferedOutputStream bufferStream = null;
        try {
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            stream = new FileOutputStream(new File(fileDir + filePath));
            bufferStream = new BufferedOutputStream(stream, 1024);
            // 创建一个段落
            XWPFParagraph p1 = document.createParagraph();
            // 设置居中
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun r1 = p1.createRun();
            // 是否加粗
            r1.setBold(true);
            // 与下一行的距离
            r1.setTextPosition(30);
//            if (EdumsConstants.FILE_TYPE_XLS.equals(type)) {
//                // 段落名称
//                r1.setText("EXCEL导入题目错误格式详解");
//            }
//            if (EdumsConstants.FILE_TYPE_DOC.equals(type)) {
//                // 段落名称
//                r1.setText("WORD导入题目错误格式详解");
//            }

            // 字体大小
            r1.setFontSize(18);// 字体大小
            // 增加换行
            r1.addCarriageReturn();
            // 创建第二个段落
            XWPFParagraph p2 = document.createParagraph();
            XWPFRun r2 = p2.createRun();
            for (int i = 0; i < content.size(); i++) {
                r2.setText(i + 1 + "、" + content.get(i));
                r2.addCarriageReturn();
            }
            // 设置字体
            r2.setFontFamily("仿宋");
            r2.setFontSize(14);// 字体大小
            document.write(stream);
            stream.close();
            bufferStream.close();
        } catch (Exception ex) {
            log.error("写word或Excel错误文件失败：{}", ex.getMessage());
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("写word或Excel错误文件失败：{}", e.getMessage());
                }
            }
            if (bufferStream != null) {
                try {
                    bufferStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("写word或Excel错误文件失败：{}", e.getMessage());
                }
            }
        }

    }

}
